package com.mediaideas.videoengine;

import android.os.Parcel;
import android.os.Parcelable;

import com.mediaideas.videoengine.constants.VideoEngineErrors;
import com.mediaideas.videoengine.utils.VideoEngineException;
import com.mediaideas.videoengine.utils.VideoEngineUtils;

/**
 * Created by Jonathan Kranz on 2016/3/5.
 * 为方便进程间通信-AIDL, 实现Parcelable接口
 */
public class AudioMixerParams implements Parcelable {
    protected final static String TAG = "AudioMixerParams";
    // 原始音频路径--录音文件路径
    protected String mSrcAudioFilePath;
    // 背景音频路径--伴奏文件路径
    protected String mBackgroundAudioFilePath;
    // 目标文件路径--混音后文件路径
    protected String mDstAudioFilePath;
    // 原始音频混音起始位置--录音音频用于混音时的起始位置
    protected long mSrcAudioStartTime;
    // 原始音频音量--录音音频混音音量
    protected float mSrcAudioVolume;
    // 背景音频音量--伴奏音频混音音量
    protected float mBackgroundAudioVolume;
    // 目标音频采样率--48000HZ
    protected int mDstAudioSampleRate;
    // 目标音频码率--采用高质量128000bit
    protected int mDstAudioBitRate;
    // 混音时长--可以进行优化到so库处理
	protected long mAudioMixerDuration;

    public final static String AUDIO_SUFFIX_AAC = ".aac";
    public final static int DEFAULT_SAMPLE_RATE = 48000; // hz
    public final static int DEFAULT_BIT_RATE = 128; // kb/s

    public String getSrcAudioFilePath() {
        return mSrcAudioFilePath;
    }

    public void setSrcAudioFilePath(String srcAudioFilePath) {
        this.mSrcAudioFilePath = srcAudioFilePath;
    }

    public String getBackgroundAudioFilePath() {
        return mBackgroundAudioFilePath;
    }

    public void setBackgroundAudioFilePath(String backgroundAudioFilePath) {
        this.mBackgroundAudioFilePath = backgroundAudioFilePath;
    }

    public String getDstAudioFilePath() {
        return mDstAudioFilePath;
    }

    public void setDstAudioFilePath(String dstAudioFilePath) throws VideoEngineException {
        if (VideoEngineUtils.isFileExists(dstAudioFilePath)) {
            String desc = VideoEngineErrors.errorDesc(VideoEngineErrors.ERROR_FILE_HAS_EXIST);
            throw  new VideoEngineException(VideoEngineErrors.ERROR_FILE_HAS_EXIST, desc);
        }
        if (!VideoEngineUtils.isFileExtensionNameValid(dstAudioFilePath)) {
            String desc = VideoEngineErrors.errorDesc(VideoEngineErrors.ERROR_FILE_EXTENSION_NAME_NOT_VALID);
            throw  new VideoEngineException(VideoEngineErrors.ERROR_FILE_EXTENSION_NAME_NOT_VALID, desc);
        }
        this.mDstAudioFilePath = dstAudioFilePath;
    }

    public long getSrcAudioStartTime() {
        return mSrcAudioStartTime;
    }

    public void setSrcAudioStartTime(long srcAudioStartTime) {
        this.mSrcAudioStartTime = srcAudioStartTime;
    }

    public float getSrcAudioVolume() {
        return mSrcAudioVolume;
    }

    public void setSrcAudioVolume(float srcAudioVolume) {
        this.mSrcAudioVolume = srcAudioVolume;
    }

    public float getBackgroundAudioVolume() {
        return mBackgroundAudioVolume;
    }

    public void setBackgroundAudioVolume(float backgroundAudioVolume) {
        this.mBackgroundAudioVolume = backgroundAudioVolume;
    }

    public int getDstAudioSampleRate() {
        return mDstAudioSampleRate;
    }

    public void setDstAudioSampleRate(int dstAudioSampleRate) {
        this.mDstAudioSampleRate = dstAudioSampleRate;
    }

    public int getDstAudioBitRate() {
        return mDstAudioBitRate;
    }

    public void setDstAudioBitRate(int dstAudioBitRate) {
        this.mDstAudioBitRate = dstAudioBitRate;
    }

	public long getAudioMixerDuration() {
		return mAudioMixerDuration;
	}

	public void setAudioMixerDuration(long audioMixerDuration) {
		mAudioMixerDuration = audioMixerDuration;
	}

    public static final Parcelable.Creator<AudioMixerParams> CREATOR = new Parcelable.Creator<AudioMixerParams>() {
        public AudioMixerParams createFromParcel(Parcel in) {
            return new AudioMixerParams(in);
        }

        public AudioMixerParams[] newArray(int size) {
            return new AudioMixerParams[size];
        }
    };


    /**
     * 默认构造方法
     */
    public AudioMixerParams() {

    }

    public AudioMixerParams(Parcel in) {
        readFromParcel(in);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeString(mSrcAudioFilePath);
        parcel.writeString(mBackgroundAudioFilePath);
        parcel.writeString(mDstAudioFilePath);
        parcel.writeLong(mSrcAudioStartTime);
        parcel.writeFloat(mSrcAudioVolume);
        parcel.writeFloat(mBackgroundAudioVolume);
        parcel.writeInt(mDstAudioSampleRate);
        parcel.writeInt(mDstAudioBitRate);
        parcel.writeLong(mAudioMixerDuration);
    }

    protected void readFromParcel(Parcel in) {
        mSrcAudioFilePath = in.readString();
        mBackgroundAudioFilePath = in.readString();
        mDstAudioFilePath = in.readString();
        mSrcAudioStartTime = in.readLong();
        mSrcAudioVolume = in.readFloat();
        mBackgroundAudioVolume = in.readFloat();
        mDstAudioSampleRate = in.readInt();
        mDstAudioBitRate= in.readInt();
        mAudioMixerDuration = in.readLong();
    }
}
